<?php
include_once("../module/sns/config.php");

if($_POST['act'] == 'saveTitle'){
    if($_POST['snsid']>0){
        $sql = "update ".SNS." set title='".$_POST['title']."' where id=".$_POST['snsid'];
        $db->query($sql);
        echo 'ok';exit;
    }
}
if($_POST['act'] == 'saveComment'){
	if($_POST['id']>0){
		$sql = "update ".SNSCOMMENTS." set content='".$_POST['comment']."' where id=".$_POST['id'];
		$db->query($sql);
		echo 'ok';exit;
	}
}
if($_GET['act']=='op'){
    if(!empty($_GET['chksns'])){
        $snsIds = "";
        foreach ($_GET['chksns'] as $snsid)
        {
            $snsIds .= $snsid.",";
        }
        if($snsIds!=""){
            if($_GET['set_status']!=""){
                $sql = "update ".SNS." set status='".$_GET['set_status']."' where id IN (".rtrim($snsIds,",").")";
                $db->query($sql);
                /*****不再加积分*****/
//                 if($_GET['set_status']=="2"){//设为推荐，更新用户积分
//                 	$sql = "select member_id,id from ".SNS." where id IN (".rtrim($snsIds,",").")";
//                 	$db->query($sql);
//                 	$member_ids = $db->getRows();
//                 	$memIds = "";
//                 	$points_log_sql = "";
//                 	foreach ($member_ids as $miv)
//                 	{
//                 		if($point_rule['SET_SNS_RECOMEND']['limit']['is_onlyOne']){
//                 			$sql = "select id from ".POINTSLOG." where member_id='{$miv['member_id']}' and from_code='SET_SNS_RECOMEND' and extension='{$miv['id']}'";
//                 			$db->query($sql);
//                 			if(!$db->fetchRow())
//                 			{
//                 				$memIds .= $miv['member_id'].",";
// 	                			$points_log_sql .="({$miv['member_id']},'SET_SNS_RECOMEND',{$point_rule['SET_SNS_RECOMEND']['point']},'".date('Y-m-d H:i:s',time())."','{$point_rule['SET_SNS_RECOMEND']['desc']}',{$miv['id']}),";
//                 			}
//                 		}else{
//                 			$memIds .= $miv['member_id'].",";
// 	                		$points_log_sql .="({$miv['member_id']},'SET_SNS_RECOMEND',{$point_rule['SET_SNS_RECOMEND']['point']},'".date('Y-m-d H:i:s',time())."','{$point_rule['SET_SNS_RECOMEND']['desc']}',{$miv['id']}),";
//                 		}
//                 	}
                	
//                 	if($memIds!=""){
// 	                	$sql = "update ".MEMBER." set `points`=`points`+( {$point_rule['SET_SNS_RECOMEND']['point']} ) where userid IN (".rtrim($memIds,',').")";
// 	                	$db->query($sql);//更新用户积分
//                 	}
//                 	if($points_log_sql!=""){
// 	                	$db->query("insert into ".POINTSLOG." (member_id,from_code,points,create_time,`desc`,extension) values ".rtrim($points_log_sql,','));//写积分日志表
//                 	}
//                 }
            }
        }
    }
}
if($_GET['act']=='comment_op'){
	if(!empty($_GET['chksnscomments'])){
		$comIds = "";
		$num = count($_GET['chksnscomments']);
		foreach ($_GET['chksnscomments'] as $comid)
		{
			$comIds .= $comid.",";
		}
		if($comIds!=""){
			if($_GET['set_status']!=""){
				$sql = "update ".SNSCOMMENTS." set status='".$_GET['set_status']."' where id IN (".rtrim($comIds,",").")";
				$db->query($sql);
				
				if($_GET['set_status']>0){
					$opsql = "update ".SNS." set comment_count=comment_count+".$num." where id=".$_GET['sns_id'];
				}else{
					$opsql = "update ".SNS." set comment_count=comment_count-".$num." where id=".$_GET['sns_id'];
				}
				$db->query($opsql);
			}
		}
	}
	msg('?m=sns&s=sns.php&act=getCommentsList&snsid='.$_GET['sns_id']);
}
if($_GET['act']=='get_pic_tags'){
//     if($_GET['sns_id']>0){
//     	//分享图片列表
//     	$sql = "select id,pic,is_cover from ".SNSPIC." where sns_id=".$_GET['sns_id']." and status>0 order by is_cover desc";
//     	$db->query($sql);
//     	$sns_pic = $db->getRows();
//         $res .= "<tr class='tagH_$_GET[sns_id]'><th></th><th width='100' colspan='3'>标签名称</th><th width='100' colspan='3'>品牌ID</th><th width='100' colspan='3'>品牌名称</th></tr>";
//     	foreach ($sns_pic as $pv){
// 	        $sql = "select b.brandid,b.brand,b.tagname from ".SNSPICTAGS." a left join ".SNSTAGS." b on a.tag_id=b.id where a.sns_pic_id=".$pv['id'];
// 	        $db->query($sql);
// 	        $tags = $db->getRows();
// 	        if(!empty($tags)){
// 	        	$index = 1;
// 	            foreach ($tags as $tv)
// 	            {
// 	            	if($index==1){
// 		                $res .= "<tr class='tagC_$_GET[sns_id]'><td rowspan='".count($tags)."'><a href='{$img_url_pre}{$pv['pic']}/100' target='_blank'><img width='100' src='{$img_url_pre}{$pv['pic']}/100' /></a></td><td colspan='3'>".$tv['tagname']."</td>"."<td colspan='3'>".$tv['brandid']."</td>"."<td colspan='3'>".$tv['brand']."</td></tr>"; 
// 	            	}else{
// 	            		$res .= "<tr class='tagC_$_GET[sns_id]'><td colspan='3'>".$tv['tagname']."</td>"."<td colspan='3'>".$tv['brandid']."</td>"."<td colspan='3'>".$tv['brand']."</td></tr>";
// 	            	}
// 	            	$index++;
// 	            }
// 	        }else{
// 	            $res .= "<tr class='tagC_$_GET[sns_id]'><td><a href='{$img_url_pre}{$pv['pic']}/100' target='_blank'><img width='100' src='{$img_url_pre}{$pv['pic']}/100' /></a></td><td colspan='9'>暂无相关数据！</td></tr>";
// 	        }
//     	}
//     	echo $res;exit;
//     }else{
//         echo "";exit;
//     }
	if($_GET['sns_id']>0){
		$res .= "<tr id='tagH_$_GET[sns_id]'><th width='100' colspan='3'>标签名称</th><th width='100' colspan='3'>品牌ID</th><th width='100' colspan='3'>品牌名称</th></tr>";
		$sql = "select b.brandid,b.brand,b.tagname from ".SNSPICTAGS." a left join ".SNSTAGS." b on a.tag_id=b.id where a.sns_id=".$_GET['sns_id'];
		$db->query($sql);
		$tags = $db->getRows();
		if(!empty($tags)){
			foreach ($tags as $tv)
			{
				$res .= "<tr class='tagC_$_GET[sns_id]'><td colspan='3'>".$tv['tagname']."</td>"."<td colspan='3'>".$tv['brandid']."</td>"."<td colspan='3'>".$tv['brand']."</td></tr>";
			}
			echo $res;exit;
		}else{
			$res .= "<tr class='tagC_$_GET[sns_id]'><td colspan='3'>暂无相关数据！</td></tr>";
			echo $res;exit;
		}
	}else{
		echo "";exit;
	}
}elseif($_GET['act']=='getFavoriteMember'){
    $de['favoriteMembers'] = array();
    if($_GET['snsid']>0){
        $sql = "select member_id from ".SNSMF." where sns_id=".$_GET['snsid'];
        $db->query($sql);
        $members = $db->getRows();
        
        if(!empty($members)){
            $mIds = "";
            foreach ($members as $mv)
            {
                $mIds .= $mv['member_id'].",";
            }
            $sql = "select * from ".MEMBER." where userid in (".rtrim($mIds,",").")";
            $db->query($sql);
            include_once("../includes/page_utf_class.php");
            $page = new Page;
            $page->listRows=20;
            $db->query($sql);
            $totalRows = $db->num_rows();
            if (!$page->__get('totalRows')){
                $db->query($sql);
                $page->totalRows = $db->num_rows();
            }
            $sql .= "  limit ".$page->firstRow.",".$page->listRows;
            
            $db->query($sql);
            $de['page'] = $page->prompt();
            
            $de['favoriteMembers'] = $db->getRows();
        }
    }
}elseif($_GET['act']=='getCommentsList'){
	$de['comment_list'] = array();
	if($_GET['snsid']>0){
		$where = "";
		if(isset($_GET['status'])){
			if($_GET['status']=="1"){
				$where = " and a.status=1";
			}elseif($_GET['status']=="0"){
				$where = " and a.status=0";
			}
		}else{
			$where = " and a.status>=0";
		}
		if(isset($_GET['member_id']) && $_GET['member_id']>0){
			$where .= " and a.member_id={$_GET['member_id']}";
		}
		$sql = "select a.*,b.userid,b.name,b.logo,(SELECT d.name FROM haiou_sns_comments c left join haiou_member d on c.member_id=d.userid WHERE c.id=a.parent_id) as aplay_name from ".SNSCOMMENTS." a left join ".MEMBER." b on a.member_id=b.userid where a.sns_id=".$_GET['snsid'].$where;
		include_once("../includes/page_utf_class.php");
		$page = new Page;
		$page->listRows=50;
		$db->query($sql);
		$totalRows = $db->num_rows();
		if (!$page->__get('totalRows')){
			$db->query($sql);
			$page->totalRows = $db->num_rows();
		}
		$sql .= "  limit ".$page->firstRow.",".$page->listRows;

		$db->query($sql);
		$de['page'] = $page->prompt();

		$de['comment_list'] = $db->getRows();
		
		//楼层
		$sql = "select id from ".SNSCOMMENTS." where sns_id=".$_GET['snsid']." order by create_time";
		$db->query($sql);
		$commentsfloorTemp = $db->getRows();
		foreach ($commentsfloorTemp as $k=>$val)
		{
			$commentsfloor[$val['id']] = $k+1;
		}
		foreach ($de['comment_list'] as $ck=>$cv)
		{
			$de['comment_list'][$ck]['floor'] = $commentsfloor[$cv['id']];
		}
	}
}else{
    $where = "";
    $order = "";
    $commentsorder = "";
    $timeorder = "desc";
    if($_GET['sns_id']>0){
    	$where .=" and a.id='{$_GET['sns_id']}'";
    }
    if($_GET['sdate']!='' && $_GET['edate']!='' && (strtotime($_GET['sdate'])<=strtotime($_GET['edate']))){
	    $where .=" and a.create_time>='".$_GET['sdate']."' and a.create_time<='".$_GET['edate']."'";
	}else{
	    if($_GET['sdate']!='' && $_GET['edate']==''){
	        $where .=" and a.create_time>='".$_GET['sdate']."'";
	    }
	    if($_GET['sdate']=='' && $_GET['edate']!=''){
	        $where .=" and a.create_time<='".$_GET['edate']."'";
	    }
	}
	if($_GET['username']!=''){
	    $where .=" and b.username like '%$_GET[username]%'";
	}
	if($_GET['tagname']!=''){
	    $where .=" and d.tagname like '%".mysql_escape_string($_GET[tagname])."%'";
	}
	if($_GET['brandid'] && $_GET['brand']){
	    $where .=" and d.brandid='".$_GET['brandid']."'";
	}elseif ($_GET['brand']!=''){
    	$where .=" and d.brand LIKE '%".mysql_escape_string($_GET['brand'])."%'";
    }
	if($_GET['status']!=""){
	    $where .=" and a.status='$_GET[status]'";
	}else{
		$where .=" and a.status>'0'";
	}
	if($_GET['order']=="desc"){
	    $order = "(select count(*) from ".SNSMF." where sns_id=a.id) desc,";
	}
	if($_GET['order']=="asc"){
	    $order = "(select count(*) from ".SNSMF." where sns_id=a.id) asc,";
	}
	if($_GET['commentsorder']=="desc"){
		$commentsorder = "(select count(*) from ".SNSCOMMENTS." where sns_id=a.id) desc,";
	}
	if($_GET['commentsorder']=="asc"){
		$commentsorder = "(select count(*) from ".SNSCOMMENTS." where sns_id=a.id) asc,";
	}
	if($_GET['timeorder']=='asc'){
	    $timeorder = "asc";
	}
	
    
    $sql = "select DISTINCT a.*,b.username as member_name,b.logo as member_photo from ".SNS." a left join ".MEMBER." b on a.member_id=b.userid left join ".SNSPICTAGS." c on a.id=c.sns_id join ".SNSTAGS." d on c.tag_id=d.id where 1 ".$where." order by ".$order.$commentsorder."create_time ".$timeorder;
    
    include_once("../includes/page_utf_class.php");
    $page = new Page;
    $page->listRows=20;
    $db->query($sql);
    $totalRows = $db->num_rows();
    if (!$page->__get('totalRows')){
        $db->query($sql);
        $page->totalRows = $db->num_rows();
    }
    $sql .= "  limit ".$page->firstRow.",".$page->listRows;
    
    $db->query($sql);
    $de['page'] = $page->prompt();
    $de['list']=$db->getRows();
    
    foreach ($de['list'] as $k=>$v)
    {
        $sql = "select count(*) as totalNum from ".SNSMF." where sns_id=".$v['id'];
        $db->query($sql);
        $de['list'][$k]['favoriteNum'] = $db->fetchField('totalNum');
        $sql = "select count(*) as totalNum from ".SNSCOMMENTS." where sns_id=".$v['id'];
        $db->query($sql);
        $de['list'][$k]['comments_num'] = $db->fetchField('totalNum');
    }
}


$tpl->assign('countpro',$totalRows);
$tpl->assign('de',$de);
$tpl->assign('img_url_pre',$img_url_pre);
$tpl->display('share_list.htm');
?>